package algorithmExample;

/**
 * @author: zhang.rongqing
 * @since: 2019/10/25
 */
public class LongestPalindormicNew {
    public String longestPalindrome(String s) {
        int start = 0, end = s.length();
        if (s.length() == 0 || s == null) {
            return "";
        }
        String maxStr = String.valueOf(s.charAt(0));
        while (start < end) {
            //偶数
            String len1 = calc(s, start, start + 1);
            //奇数
            String len2 = calc(s, start, start);
            String len = len1.length() > len2.length() ? len1 : len2;
            maxStr = maxStr.length() > len.length() ? maxStr : len;
            start++;
        }
        return maxStr;
    }

    public String calc(String str, int L, int R) {
        while (L >= 0 && R < str.length() && str.charAt(L) == str.charAt(R)) {
            L--;
            R++;
        }
        return str.substring(L + 1, R);
    }

    public static void main(String[] args) {
        String str = "abac";
        LongestPalindormicNew longestPalindormicNew = new LongestPalindormicNew();
        System.out.println(longestPalindormicNew.longestPalindrome(str));
    }
}
